/**
 * Created by Administrator on 2015/11/12.
 */
define("wangwu/ceshi/MouseTip",
    [
        'dojo/_base/declare',
        'dojo/_base/lang',
        'dojo/on',
        'dojo/dom-construct',
        'dojo/query',
        'dojo/dom-attr',
        'dojo/dom-style',
        'dojo/_base/connect',
        "dojo/mouse",

        'esri/layers/GraphicsLayer',
        'esri/graphic',
        'esri/Color',
        'esri/geometry/Point',
        'esri/symbols/SimpleLineSymbol',
        'esri/Evented',
        'esri/geometry/screenUtils',
        'esri/geometry/geometryEngine',

        'd3/d3'
    ],
    function(declare,lang, on,domConstruct,djQuery,domattr,domStyle,connect,mouse,
             GraphicsLayer,graphic,Color,Point,SimpleLineSymbol,Evented,screenUtils,geometryEngine,
             D3){
        var d = declare(null,{
            declaredClass:'wangwu.model.Locate',
            constructor:function(map,opt){
                this.map = map;
                this.option={
                    string:"鼠标提示"
                };
                (opt)?lang.mixin(this.option,opt):0;
                this.domId = 'MouseTip-'+parseInt(Math.random()*1000);
                this.initialized = false;

                this.opt = {
                    rectRadius:5,//Բ��
                    rectWidth:150,//���ο��
                    rectOffsetX:0,//����������xƫ��
                    rectOffsetY:10,//����top yƫ��
                    rectFillColor:'rgba(144,155,165,0.6)',//���������ɫ
                    rectFillHighlightColor:'rgba(20,20,220,0.8)',/////////////
                    rectStrokeColor:'rgba(33,33,33,0.2)',//���������ɫ
                    rectStrokeHighlightColor:'rgba(20,20,20,1)',
                    rectStrokeWidth:1,//���������
                    labelVerticalAlign:4,//��ǩ��ֱ���
                    labelsPaddingTop:2,//��ǩ�������������ļ��
                    labelsPaddingBottom:2,
                    labelsPaddingLeft:2,
                    labelsPaddingRight:2,
                    fontSize:13,//�����С
                    fontColor:'rgba(255,255,0,1)',
                    tipFillColor:'rgba(255,255,255,1)',
                    tipfontColor:'rgba(0,0,0,1)'
                };
                //调试用
                //this.buttonId = 'testDojo-'+parseInt(Math.random()*1000);
                //domConstruct.create('div', {
                //    'id':'aaaa1111',
                //    'style':'position:absolute;right:20px;top:100px;width:100px',
                //    'innerHTML':'<input type="button" value="开启提示" id="'+this.buttonId+'"/>'
                //}, map.id);
                //var button = djQuery('#'+this.buttonId);
                //on(button, 'click', lang.hitch(this, this.init));
                //
                //this.buttonId01 = 'testDojo01-'+parseInt(Math.random()*1000);
                //domConstruct.create('div', {
                //    'id':'aaaa2222',
                //    'style':'position:absolute;right:20px;top:150px;width:100px',
                //    'innerHTML':'<input type="button" value="关闭提示" id="'+this.buttonId01+'"/>'
                //}, map.id);
                //var button01 = djQuery('#'+this.buttonId01);
                //on(button01, 'click', lang.hitch(this, this.clearAll));

            },
            //-----------------------public----------------------------------

            init:function(){
                if (this.initialized) return;
                this.initialized = true;
                this._init();
            },
            //开启事件侦听
            openEventListen:function(){
                this.pan = on(this.map, 'pan', lang.hitch(this, this._pan));
                this.panEnd = on(this.map, 'pan-end', lang.hitch(this, this.refresh));
                this.zoomStart = on(this.map, 'zoom-start', lang.hitch(this, this._zoomStart));
                this.zoomEnd = on(this.map, 'zoom-end', lang.hitch(this, this._zoomEnd));
                this.extentChange = on(this.map,'extent-change',lang.hitch(this,this.refresh));
                this.mouseMove = on(this.map,'mouse-move',lang.hitch(this,this._addTip));
                this.mouseOut = on(this.map,'mouse-out',lang.hitch(this,this._clearTip));
                //var mapDiv = djQuery("#mapDiv");
                //this.mouseLeave = on(mapDiv,mouse.leave,lang.hitch(this,this._clearTip));
            },
            //关闭事件侦听
            closeEventListen:function(){
                (this.pan)?this.pan.remove():0;
                (this.panEnd)?this.panEnd.remove():0;
                (this.zoomStart)?this.zoomStart.remove():0;
                (this.zoomEnd)?this.zoomEnd.remove():0;
                (this.extentChange)?this.extentChange.remove():0;
                (this.mouseMove)?this.mouseMove.remove():0;
                (this.mouseOut)?this.mouseLeave.remove():0;
                //(this.mouseLeave)?this.mouseLeave.remove():0;
            },
            refresh:function(){
                this._refresh();
            },
            getXY:function(e){
                var exte=this.map.extent;
                console.log(exte);
                console.log(e.mapPoint);

                return [exte.xmin-0.01,exte.xmax+0.01,exte.ymin-0.01,exte.ymax+0.01]
            },
            clearAll:function(){
                console.log("关闭");
                this._clearALL();
            },
            //-----------------------private----------------------------------
            _init:function(){
                D3.select('#'+this.map.id+'_gc')
                    .append('g')
                    .attr({
                        'id':this.domId
                    });
                this.openEventListen();
            },
            _addTip:function(e){
                this._clearTip();
                this._tip(e);
            },
            _tip:function(e){
                var o = this.opt;
                var shapeHeight = o.labelsPaddingTop + (o.fontSize + o.labelVerticalAlign) + o.labelsPaddingBottom;
                var shapeWidth = o.rectWidth;
                var x = 0, y = 0;
                var map = this.map;
                var mapExtent = map.extent;
                var mapWidth = map.width;
                var mapHeight = map.height;
                var xy = screenUtils.toScreenPoint(mapExtent, mapWidth, mapHeight, e.mapPoint);

                var _mvr = map.__visibleRect;
                x = xy.x - _mvr.x - shapeWidth / 2 + o.rectOffsetX;
                y = xy.y - _mvr.y + o.rectOffsetY;
                var node = D3.select('#' + this.domId);
                node.attr({
                    'transform':D3.select('#'+this.map.id+'_graphics_layer').attr('transform')
                });

                var labelY = y + o.labelsPaddingTop + o.fontSize + (o.fontSize + o.labelVerticalAlign) || 0;
                var fstring1 = this.option.string;
                var text1 = this._trim(fstring1);
                var labelX = x + o.labelsPaddingLeft + 76 - ((text1.length) * parseInt(o.fontSize) + 10) / 2 || 0;

                node.append('rect')
                    .attr({
                        'x': labelX+55,
                        'y': y+5,
                        'width': (text1.length) * parseInt(o.fontSize) + 10,
                        'height': shapeHeight+5,
                        'fill': o.tipFillColor,
                        'stroke': o.rectStrokeColor,
                        'stroke-width': o.rectStrokeWidth,
                        'rx': o.rectRadius,
                        'ry': o.rectRadius
                    });


                var txtColor = o.tipfontColor;
                node.append('text')
                    .attr({
                        'x': labelX+60,
                        'y': labelY-10,
                        'fill': txtColor,
                        'font-size': o.fontSize,
                        'dx': o.labelDx
                    })
                    .text(text1);
            },
            _trim:function(str){
                return str.replace(/(^\s+)|(\s+$)/g, "");
            },
            _clearTip:function(){
                (djQuery('#'+this.domId+' text').length > 0)?dojo.empty(this.domId):0;
                (djQuery('#'+this.domId+' rect').length > 0)?dojo.empty(this.domId):0;
            },
            _refresh:function(){
                this.map.setMapCursor("pointer");
            },

            _pan:function(e){
                var dx = e.delta.x;
                var dy = e.delta.y;
                var _dx = this.map.__visibleRect.x+dx;
                var _dy = this.map.__visibleRect.y+dy;
                D3.select('#'+this.domId)
                    .attr({
                        'transform':'matrix(1,0,0,1,'+_dx+','+_dy+')'
                    });
            },
            _zoomStart:function(){
                this._clearTip();
            },
            _zoomEnd:function(){
                D3.select('#'+this.domId)
                    .attr({
                        'transform':'matrix(1,0,0,1,0,0)'
                    })
            },
            _hide:function(){
                domStyle.set(this.domId, 'display', 'none');
                this.visible = false;
            },
            _show:function(){
                domStyle.set(this.domId, 'display', 'block');
                this.visible = true;
            },
            _hideFeatures:function(){
                domStyle.set(this.domId, 'display', 'none');
            },
            _showFeatures:function(){
                domStyle.set(this.domId, 'display', 'block');
            },
            _clearALL:function(){
                this._clearTip();
                this.closeEventListen();
                this.initialized = false;
            }
        });
        return d;

    })